'use strict';

exports.__esModule = true;

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _loading = require('../loading');

var _loading2 = _interopRequireDefault(_loading);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

exports.default = function (loader, componentProps) {
  var _class, _temp2;

  return _temp2 = _class = function (_PureComponent) {
    (0, _inherits3.default)(AsyncComponent, _PureComponent);

    function AsyncComponent() {
      var _temp, _this, _ret;

      (0, _classCallCheck3.default)(this, AsyncComponent);

      for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, _PureComponent.call.apply(_PureComponent, [this].concat(args))), _this), _this.state = {
        Component: AsyncComponent.Component
      }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
    }

    AsyncComponent.prototype.componentWillMount = function componentWillMount() {
      var _this2 = this;

      if (!this.state.Component) {
        loader().then(function (Component) {
          AsyncComponent.Component = Component;

          _this2.setState(function () {
            return {
              Component: Component
            };
          });
        });
      }
    };

    AsyncComponent.prototype.componentWillUnmount = function componentWillUnmount() {
      this.loadingInstance && _loading2.default.hide();
    };

    AsyncComponent.prototype.render = function render() {
      var Component = this.state.Component;


      if (Component) {
        if (this.loadingInstance) {
          // Loading.hide()
          this.loadingInstance = null;
        }

        return _react2.default.createElement(Component, (0, _extends3.default)({}, this.props, componentProps));
      }

      if (!this.loadingInstance) {
        // this.loadingInstance = Loading.show()
      }

      return null;
    };

    return AsyncComponent;
  }(_react.PureComponent), _class.Component = null, _temp2;
};